package com.ruoyi.batchinsert;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.system.domain.SysOperLog;
import com.ruoyi.system.mapper.SysOperLogMapper;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

public class InsertTask implements Callable<Boolean> {
    private final List<SysOperLog> batch;
    private final CountDownLatch latch;
    private final SysOperLogMapper sysOperLogMapper;

    // 修改构造函数，将CountDownLatch和SysOperLogMapper都作为普通参数传入
    public InsertTask(List<SysOperLog> batch, CountDownLatch latch, SysOperLogMapper sysOperLogMapper) {
        this.batch = batch;
        this.latch = latch;
        this.sysOperLogMapper = sysOperLogMapper;
    }

    @Override
    public Boolean call() {
        try {
            // 使用MyBatis-Plus的批量插入方法
            if (CollectionUtils.isNotEmpty(batch)) {
                sysOperLogMapper.insertBatchSomeColumn(batch);
            }
            return true;
        } catch (Exception e) {
            System.err.println("批量插入失败: " + e.getMessage());
            return false;
        } finally {
            // 无论成功与否，计数减一
            latch.countDown();
        }
    }
}
